/*
 * Copyright 2009 Original Author(s)
 * 
 * This file is part of Kommando
 * 
 * Kommando is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Kommando is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Kommando.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.kommando.filesystem.win32.actions;

import javax.swing.Icon;

import org.kommando.core.action.AbstractAction;
import org.kommando.core.action.ActionExecutionException;
import org.kommando.core.catalog.CatalogObject;
import org.kommando.filesystem.core.DiskObject;

/**
 * Action to eject a disk.
 * 
 * @author Peter De Bruycker
 */
public class EjectAction extends AbstractAction {

    // TODO check if disk can be ejected
    // TODO if disk is a cdrom/dvd drive, use autoit3 -> DriveGetType("drive") -> "Removable" -> USB_Disk_Ejector,
    // "CDROM" -> AutoIt3 CDTray("drive", "open")

    private static final String ID = "file:disk:eject";

    @Override
    public CatalogObject execute(CatalogObject direct, CatalogObject indirect) throws ActionExecutionException {
        DiskObject diskObject = direct.getExtension(DiskObject.class);

        try {
            Process process = new ProcessBuilder(System.getenv("comspec"), "/c",
                    "C:\\UserTemp\\projects\\kommando\\trunk\\incubator\\org.kommando.filesystem.win32\\USB_Disk_Eject.exe", "/REMOVELETTER", diskObject
                            .getFile().getAbsolutePath().substring(0, 1)).start();

            process.waitFor();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public Class<?>[] getDirectExtensionTypes() {
        return new Class<?>[] { DiskObject.class };
    }

    @Override
    public String getDescription() {
        return "Eject this disk";
    }

    @Override
    public Icon getIcon() {
        // TODO icon
        return null;
    }

    @Override
    public String getName() {
        return "Eject";
    }

    @Override
    public String getId() {
        return ID;
    }

}
